package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Likeb;
import cn.wolfcode.trip.base.mapper.LikebMapper;
import cn.wolfcode.trip.base.service.ILikebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class LikebServiceImpl implements ILikebService {
    @Autowired
    private LikebMapper likebMapper;

    @Override
    public Likeb selectLikebByUserId(Long userId, Long strategyId) {
       Likeb likeb=likebMapper.selectState(userId,strategyId);
        return likeb;
    }

    @Override
    public void save(Long userId, Long strategyId) {
        //通过页面传入的两个id 查询是否有点赞记录,如果有点赞记录,删除,如果没有,保存记录
        Likeb likeb=likebMapper.selectState(userId,strategyId);
        if(likeb==null){
            //做保存操作
            Likeb likeb1 = new Likeb();
            likeb1.setStrategyId(strategyId);
            likeb1.setUserId(userId);
            likebMapper.insert(likeb1);
        }else if(likeb !=null){
            //点赞数量减一

            //删除这条点赞状态
            likebMapper.deleteByP2Id(userId,strategyId);
        }

    }

    @Override
    public void delete(Long userId, Long strategyId) {
        //先维护中间表关系,删除中间表的点赞记录

        likebMapper.deleteByP2Id(userId,strategyId);
    }


    @Override
    public int getNumByLike(Long strategyId) {

       int num= likebMapper.countByLikeb(strategyId);

        return num;
    }


}
